package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.dto.SysUserInfo;
import com.itheima.ydd.dto.UserDataDto.UserDataArticleDto;
import com.itheima.ydd.dto.UserDataDto.UserDataGetDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.IndexMapper;
import com.itheima.ydd.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @author maorf
 * @version 1.0
 */
@Service
public class IndexServiceImpl extends ServiceImpl<IndexMapper, SysUser> implements IndexService {

    @Autowired
    SysUserService userService;
    @Autowired
    SysRoleService roleService;
    @Autowired
    MenuService menuService;
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddCateService yddCateService;
    @Autowired
    private YddUserDataService yddUserDataService;

    @Override
    public Map<String, Object> profile() {
        HashMap<String, Object> map = new HashMap<>();
        //1. 查询用户信息
        String userId = UserHolder.getUserId();
        SysUser user = userService.getById(UserHolder.getUserId());
        SysUserInfo userInfo = new SysUserInfo();
        BeanUtils.copyProperties(user, userInfo);

        //查询用户角色id集合: role_id
        String[] rowIds = user.getRoleId().replace("[", "")
                .replace("]", "")
                .replace("\"", "")
                .split(",");
        List<String> ids = new ArrayList<>();
        Collections.addAll(ids, rowIds);
        userInfo.setRoleIdList(ids);
        map.put("userInfo", userInfo);

        //查询用户角色名称集合: id
        String id = UserHolder.get().getId();
        SysUser user1 = userService.getById(id);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getId, user1.getRoleId());
        List<SysRole> list = roleService.list(wrapper);
        List<String> roleNameList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(list)) {
            for (SysRole sysRole : list) {
                roleNameList.add(sysRole.getName());
            }
        }
        userInfo.setRoleNameList(roleNameList);

        //2. 查询角色菜单集合
        List<Menu> menuList = menuService.menuList(rowIds);
        map.put("menuList", menuList);


        //region #dictsList 不知道干嘛用的，结果直接为原本数据
        HashMap<String, Object> dictsList = new HashMap<>();
        HashMap<String, String> interface_type = new HashMap<>();
        interface_type.put("get", "get");
        interface_type.put("post", "post");
        dictsList.put("interface_type", interface_type);

        ArrayList<String> table_type = new ArrayList<>();
        table_type.add("单表");
        table_type.add("主表");
        table_type.add("附表");
        table_type.add("树结构表");
        dictsList.put("table_type", table_type);

        ArrayList<String> order_type = new ArrayList<>();
        order_type.add("支付宝");
        order_type.add("微信");
        dictsList.put("order_type", order_type);

        ArrayList<String> report_type = new ArrayList<>();
        report_type.add("正常报告");
        report_type.add("异常报告");
        dictsList.put("report_type", report_type);

        HashMap<String, String> theme = new HashMap<>();
        theme.put("ace", "ACE风格");
        dictsList.put("theme", theme);
        map.put("dictsList", dictsList);

        return map;
    }

    @Override
    public ResponseEntity<Result> findById(Integer id) {
        UserDataGetDto userDataGetDto = new UserDataGetDto();
        Random r = new Random();
        //先设置当前id的数据封装到info里面
        YddArticle yddArticle = yddArticleService.getById(id);
        YddCate yddCate = yddCateService.getById(yddArticle.getCateid());
        UserDataArticleDto userDataArticleDto = new UserDataArticleDto();
        BeanUtils.copyProperties(yddArticle, userDataArticleDto);
        userDataArticleDto.setCatename(yddCate.getCatename());
        LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUserData::getUserId, yddArticle.getUserId()).eq(YddUserData::getArticleId, yddArticle.getId());
        List<YddUserData> list = yddUserDataService.list(wrapper);
        userDataArticleDto.setIsCollect(true);
        userDataArticleDto.setIsLike(true);
        if (ObjectUtils.isEmpty(list)) {
            userDataArticleDto.setIsCollect(false);
            userDataArticleDto.setIsLike(false);
        } else if (list.size() == 1) {
            Integer type = list.get(0).getType();
            if (type == 1) {
                userDataArticleDto.setIsLike(false);
            } else {
                userDataArticleDto.setIsCollect(false);
            }
        }
        userDataGetDto.setInfo(userDataArticleDto);

        //再获取上一个id的数据
        YddArticle prevYddArticle = yddArticleService.getById(yddArticle.getId() - 1);
        if (ObjectUtils.isEmpty(prevYddArticle)) {
            List<String> stringList = new ArrayList<>();
            userDataGetDto.setPrev(stringList);
        } else {
            List<YddArticle> yddArticleListPrev = new ArrayList<>();
            yddArticleListPrev.add(prevYddArticle);
            userDataGetDto.setPrev(yddArticleListPrev);
        }
        //获取下一个id的数据
        YddArticle nextYddArticle = yddArticleService.getById(yddArticle.getId() + 1);
        if (ObjectUtils.isEmpty(nextYddArticle)) {
            userDataGetDto.setNext(null);
        } else {
            List<YddArticle> yddArticleListNext = new ArrayList<>();
            yddArticleListNext.add(nextYddArticle);
            userDataGetDto.setNext(yddArticleListNext);
        }
        //推荐文章数据
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddArticle::getIshot, 1);
        List<YddArticle> yddArticleList = yddArticleService.list(queryWrapper);
        List<YddArticle> recommend = new ArrayList<>();
        for (int i = 0; i < yddArticleList.size(); i++) {
            if (recommend.size() < 8) {
                recommend.add(yddArticleList.remove(r.nextInt(yddArticleList.size())));
            }
        }
        userDataGetDto.setRecommend(recommend);
        return ResponseEntity.ok(new Result(0, "", userDataGetDto));
    }
}
